package com.hlxt.nj.common;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.ServletRequestUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

public class PageBeanUtil
{
	public static PageBean getPageBean(HttpServletRequest request){
		java.io.BufferedReader rd;
		PageBean pageBean = new PageBean(10);
		try {
			StringBuffer line=new StringBuffer();
			String str = "";
			rd = request.getReader();
			while ( (str=rd.readLine())!=null) {
				line.append(str);
			}
		  
		  str = line.toString();
		  Map<String,String> pageParamMap = parserToMap(str);
		  
		  
//		  int limit = ServletRequestUtils.getIntParameter(request, "limit", 20);
//		  int start = ServletRequestUtils.getIntParameter(request, "offset", 0);
		  int limit = Integer.parseInt(pageParamMap.get("limit"))==0?20:Integer.parseInt(pageParamMap.get("limit"));
		  int start = Integer.parseInt(pageParamMap.get("offset"))==0?0:Integer.parseInt(pageParamMap.get("offset"));
		  
		  pageBean.setCurrentPage(start/limit+1);
		  pageBean.setLength(limit);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(pageBean.getCurrentPage()==0){
				pageBean.setCurrentPage(1);
			}
			return pageBean;
		}
	  }
	/*
	 *默认分页 
	 */
  /*public static PageBean getPageBean(HttpServletRequest request){
	  int limit = ServletRequestUtils.getIntParameter(request, "limit", 20);
	  int start = ServletRequestUtils.getIntParameter(request, "offset", 0);
	  PageBean pageBean = new PageBean(10);
	  pageBean.setCurrentPage(start/limit+1);
	  pageBean.setLength(limit);
	  return pageBean;
  }*/

  public static void replacePageBeanList(PageBean pageBean, Class newClass) throws InstantiationException, IllegalAccessException
  {
    List list = pageBean.getResults();
    List newList = new ArrayList();
    Iterator it = list.iterator();
    while (it.hasNext()) {
      Object obj = it.next();
      Object newObj = newClass.newInstance();
      BeanUtils.copyProperties(obj, newObj);
      newList.add(newObj);
    }
    pageBean.setResults(newList);
    list.clear();
  }

  public static PageBean getMiniPageBean(HttpServletRequest request) {
    int pageSize = 5;
    int pageLength = ServletRequestUtils.getIntParameter(request, "pageSize",
      pageSize);

    int currentPage = ServletRequestUtils.getIntParameter(request, "currentPage",
      1);
    //length
    PageBean pageBean = new PageBean(10);
    pageBean.setCurrentPage(currentPage);
    pageBean.setLength(pageLength);
    return pageBean;
  }
  
  public static Map parserToMap(String s){  
	    Map map=new HashMap();  
	    JSONObject json= JSON.parseObject(s);
	    Set set=json.keySet();
	    Iterator keys = set.iterator();
	    while(keys.hasNext()){  
	        String key=(String) keys.next();  
	        String value=json.get(key).toString();
	        if(value.startsWith("{")&&value.endsWith("}")){  
	            map.put(key, parserToMap(value));  
	        }else{  
	            map.put(key, value);  
	        }  
	  
	    }  
	    return map;  
	} 
}